﻿// Accord Statistics Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
//    This library is free software; you can redistribute it and/or
//    modify it under the terms of the GNU Lesser General Public
//    License as published by the Free Software Foundation; either
//    version 2.1 of the License, or (at your option) any later version.
//
//    This library is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//    Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public
//    License along with this library; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
//

namespace Accord.Statistics.Models.Markov
{
    using System;

    /// <summary>
    ///   Common interface for Hidden Markov Models.
    /// </summary>
    /// 
    public interface IHiddenMarkovModel
    {

        /// <summary>
        ///   Calculates the most likely sequence of hidden states
        ///   that produced the given observation sequence.
        /// </summary>
        /// <remarks>
        ///   Decoding problem. Given the HMM M = (A, B, pi) and  the observation sequence 
        ///   O = {o1,o2, ..., oK}, calculate the most likely sequence of hidden states Si
        ///   that produced this observation sequence O. This can be computed efficiently
        ///   using the Viterbi algorithm.
        /// </remarks>
        /// <param name="observations">
        ///   A sequence of observations.</param>
        /// <param name="logLikelihood">
        ///   The state optimized probability.</param>
        /// <returns>
        ///   The sequence of states that most likely produced the sequence.
        /// </returns>
        /// 
        int[] Decode(Array observations, out double logLikelihood);

        /// <summary>
        ///   Calculates the probability that this model has generated the given sequence.
        /// </summary>
        /// <remarks>
        ///   Evaluation problem. Given the HMM  M = (A, B, pi) and  the observation
        ///   sequence O = {o1, o2, ..., oK}, calculate the probability that model
        ///   M has generated sequence O. This can be computed efficiently using the
        ///   Forward algorithm. </remarks>
        /// <param name="observations">
        ///   A sequence of observations. </param>
        /// <returns>
        ///   The probability that the given sequence has been generated by this model.
        /// </returns>
        /// 
        double Evaluate(Array observations);

        /// <summary>
        ///   Gets the number of states of this model.
        /// </summary>
        /// 
        // [Obsolete("Please use NumberOfStates instead.")]
        int States { get; }


        /// <summary>
        ///   Gets the initial probabilities for this model.
        /// </summary>
        /// 
        [Obsolete("Please use the LogInitial property instead.")]
        double[] Probabilities { get; }

        /// <summary>
        ///   Gets the log of the initial probabilities (log(pi)) for this model.
        /// </summary>
        /// 
        double[] LogInitial { get; }

        /// <summary>
        ///   Gets the log of the transition matrix (log(A)) for this model.
        /// </summary>
        /// 
        [Obsolete("Please use the LogTransitions property instead.")]
        double[,] Transitions { get; }

        /// <summary>
        ///   Gets the log of the transition matrix (log(A)) for this model.
        /// </summary>
        /// 
        double[][] LogTransitions { get; }


        /// <summary>
        ///   Gets or sets a user-defined tag.
        /// </summary>
        /// 
        object Tag { get; set; }



        /// <summary>
        ///   Calculates the probability of each hidden state for each
        ///   observation in the observation vector.
        /// </summary>
        /// 
        /// <remarks>
        ///   If there are 3 states in the model, and the <paramref name="observations"/>
        ///   array contains 5 elements, the resulting vector will contain 5 vectors of
        ///   size 3 each. Each vector of size 3 will contain probability values that sum
        ///   up to one. By following those probabilities in order, we may decode those
        ///   probabilities into a sequence of most likely states. However, the sequence
        ///   of obtained states may not be valid in the model.
        /// </remarks>
        /// 
        /// <param name="observations">A sequence of observations.</param>
        /// 
        /// <returns>A vector of the same size as the observation vectors, containing
        ///  the probabilities for each state in the model for the current observation.
        ///  If there are 3 states in the model, and the <paramref name="observations"/>
        ///  array contains 5 elements, the resulting vector will contain 5 vectors of
        ///  size 3 each. Each vector of size 3 will contain probability values that sum
        ///  up to one.</returns>
        /// 
        double[][] Posterior(Array observations);

        /// <summary>
        ///   Calculates the probability of each hidden state for each observation 
        ///   in the observation vector, and uses those probabilities to decode the
        ///   most likely sequence of states for each observation in the sequence 
        ///   using the posterior decoding method. See remarks for details.
        /// </summary>
        /// 
        /// <remarks>
        ///   If there are 3 states in the model, and the <paramref name="observations"/>
        ///   array contains 5 elements, the resulting vector will contain 5 vectors of
        ///   size 3 each. Each vector of size 3 will contain probability values that sum
        ///   up to one. By following those probabilities in order, we may decode those
        ///   probabilities into a sequence of most likely states. However, the sequence
        ///   of obtained states may not be valid in the model.
        /// </remarks>
        /// 
        /// <param name="observations">A sequence of observations.</param>
        /// <param name="path">The sequence of states most likely associated with each
        ///   observation, estimated using the posterior decoding method.</param>
        /// 
        /// <returns>A vector of the same size as the observation vectors, containing
        ///  the probabilities for each state in the model for the current observation.
        ///  If there are 3 states in the model, and the <paramref name="observations"/>
        ///  array contains 5 elements, the resulting vector will contain 5 vectors of
        ///  size 3 each. Each vector of size 3 will contain probability values that sum
        ///  up to one.</returns>
        /// 
        double[][] Posterior(Array observations, out int[] path);
    }

}
